package com.bidevalution.controller.api;

import com.bidevalution.business.dto.VoteResultDto;
import com.bidevalution.business.service.*;
import com.bidevalution.controller.util.RequestUtil;
import com.bidevalution.controller.vo.BeExpertVoteResultVO;
import com.bidevalution.dao.entity.*;
import com.bidevalution.support.context.Constants;
import com.bidevalution.support.context.ResponseObject;
import com.bidevalution.support.context.WebSocketOpTypeEnum;
import com.bidevalution.support.factory.WebSocketService;
import com.bidevalution.support.util.StringUtil;
import com.bidevalution.support.websocket.WebSocketCached;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;
import com.qtp.core.util.OConvertUtils;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 专家投票结果控制层 Created by glodon on 2019-08-15.
 */
@Log4j2
@RestController
@RequestMapping("/beExpertVoteResult")
@Api(tags = "推选组长投票结果")
public class BeExpertVoteResultController extends BaseController {

    @Autowired
    private BeExpertVoteResultService beExpertVoteResultService;
    @Autowired
    private WebSocketService webSocketService;
    @Autowired
    private BeSectionExpertInfoService beSectionExpertInfoService;
    @Autowired
    private BeSectionExpertProgressService beSectionExpertProgressService;
    @Autowired
    private BeSectionStepService beSectionStepService;
    @Autowired
    private BeSectionReappraisalRecordService beSectionReappraisalRecordService;
    @Autowired
    private BeSectionMethodService beSectionMethodService;
    @Value("${REQUEST_TYPE}")
    private String requestType;

    private static final String UPDATE_SUBMIT = "UPDATE_SUBMIT_";

    @PostMapping("/voteExpert")
    @ApiOperation(value = "推选组长-专家投票", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionStepId", value = "标段环节主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteResult", value = "推选组长专家id,pk-bs_section_expert-seq_id,替补和重评:1同意 0 不同意", required = true),})
    public Response<String> voteExpert(@RequestParam("sectionId") String sectionId,
                                       @RequestParam("sectionStepId") String sectionStepId, @RequestParam("voteType") String voteType,
                                       @RequestParam("voteResult") String voteResult) {
        try {
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                voteExpertMethod(sectionId, sectionStepId, voteType, voteResult, tsUser, Constants.ONE);
                return Response.ok("投票成功");
            } else {
                return Response.fail("获取专家账号登录信息失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("投票失败");
        }
    }

    @PostMapping("/voteExpertReviewEnd")
    @ApiOperation(value = "专家投票-评审结束按钮（目前仅用于评审结束按钮）", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionStepId", value = "标段环节主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteResult", value = "推选组长专家id,pk-bs_section_expert-seq_id,替补和重评:1同意 0 不同意", required = true),})
    public Response<String> voteExpertReviewEnd(@RequestParam("sectionId") String sectionId,
                                                @RequestParam("sectionStepId") String sectionStepId, @RequestParam("voteType") String voteType,
                                                @RequestParam("voteResult") String voteResult) {
        try {
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                log.info("方法被调用");
                log.info("sectionId[" + sectionId + "]sectionStepId[" + sectionStepId + "]voteType[" + voteType + "]专家姓名[" + tsUser.getLoginName() + "]");
                voteExpertMethod(sectionId, sectionStepId, voteType, voteResult, tsUser, Constants.TWO);
                return Response.ok("投票成功");
            } else {
                return Response.fail("获取专家账号登录信息失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("投票失败");
        }
    }

    /**
     * 专家投票方法
     *
     * @param sectionId
     * @param sectionStepId
     * @param voteType
     * @param voteResult
     * @param tsUser
     * @param flag          1、其它投票调用 2、评审结束按钮调用（因为按钮是通过动态配置的，评审结束按钮需要执行更新操作）
     * @throws Exception
     */
    private void voteExpertMethod(String sectionId, String sectionStepId, String voteType, String voteResult, TSUser tsUser, int flag) throws Exception {
        BeSectionExpertInfo _beSectionExpertInfo = beSectionExpertInfoService.selectByLoginName(sectionId,
                tsUser.getLoginName());
        if (beExpertVoteResultService.voteExpert(_beSectionExpertInfo.getSeqId(), sectionId, voteResult,
                sectionStepId, voteType, flag)) {
            //重评需要根据submitCode去查询数据（每重评一次submitCode都会变）
        
            BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(sectionId);
            String submitCode = beSectionMethod.getSubmitCode();
            String computerVoteResult = beExpertVoteResultService.computerVoteResult(sectionId, voteType, submitCode);
            
            
            BeSectionStep preStep = null;
            BeSectionStep currStep = null;
            if(voteType.equals("1")) {
            	BeSectionReappraisalRecord beSectionReappraisalRecord = new BeSectionReappraisalRecord();
        		beSectionReappraisalRecord.setSectionId(sectionId);
        		beSectionReappraisalRecord.setSubmitType(voteType);
        		beSectionReappraisalRecord.setSubmitCode(submitCode);
        		beSectionReappraisalRecord = beSectionReappraisalRecordService.queryRecordByObject(beSectionReappraisalRecord);
        		if(beSectionReappraisalRecord != null) {
                    currStep = beSectionStepService.getSectionStepBySeqId(beSectionReappraisalRecord.getSectionStepId());
                    List<BeSectionStep> preStepList = beSectionStepService.getPretStepList(sectionId, currStep);
                    if(preStepList.size()>0) {
                    	preStep = preStepList.get(preStepList.size() - 1);
                    }
        		}else {
            		throw new Exception("没有存在替补环节");
        		}
            }
            if ("1".equals(computerVoteResult)) {
                //多数专家同发起数据更新操作
            	beSectionReappraisalRecordService.updateRecordByObject(sectionId,submitCode,voteType,currStep,preStep);
            }
           
            for (BeSectionExpertInfo beSectionExpertInfo : beSectionExpertInfoService
                    .getExpertListBySectionId(sectionId, voteType, submitCode)) {
            	//发送投票通知到前端computerVoteResult:0表示投票完成未通过 1 投票完成已通过 
                if (WebSocketCached.getExpertChannelContextMap().get(beSectionExpertInfo.getSeqId()) != null) {
                    List<String[]> idCards = beSectionExpertInfoService.findMd5ByLoginNames(sectionId);
                	if(voteType.equals("1")) {
                		webSocketService.send(
                                WebSocketCached.getExpertChannelContextMap().get(beSectionExpertInfo.getSeqId()),
                                new ResponseObject(WebSocketOpTypeEnum.EXPERT_ALTER_COUNT).setData("stepCode",  preStep.getStepCode())
                                .setData("computerVoteResult", computerVoteResult).setTodoList(idCards));
                	}else {
                		webSocketService.send(
                                WebSocketCached.getExpertChannelContextMap().get(beSectionExpertInfo.getSeqId()),
                                new ResponseObject(WebSocketOpTypeEnum.EXPERT_FINISH).setTodoList(idCards));
                	}
                	 
                }
            }
        } else {
            throw new Exception("投票失败");
        }
    }

    @PostMapping("/startVote")
    @ApiOperation(value = "替补和重评时专家发起投票", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionStepId", value = "标段环节主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true)})
    public Response<String> startVote(@RequestParam("sectionStepId") String sectionStepId,
                                      @RequestParam("voteType") String voteType) {
        try {
            BeSectionStep beSectionStep = beSectionStepService.getSectionStepBySeqId(sectionStepId);
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null && beSectionStep != null) {
                BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(beSectionStep.getSectionId());
                String submitCode = beSectionMethod.getSubmitCode();
                if("1".equals(voteType)) {
                	//非当前评审环节
                	if(!"1".equals(beSectionStep.getIsActive())) {
                		return Response.fail("替补操作已达上线");
                	}
                	if("0".equals(beExpertVoteResultService.computerVoteResult(beSectionStep.getSectionId(), voteType, submitCode))) {
                     	return Response.ok("已经存在未完成投票的替补记录");
                	}
                }
                
                //删除所有之前的投标记录
                beExpertVoteResultService.delBySectionId(beSectionStep.getSectionId(), voteType, submitCode);
                //保存重评和替补发起记录
                beSectionReappraisalRecordService.saveReappraisalRecord(beSectionStep, submitCode,voteType,userGuid);
                sendMsg(tsUser, beSectionStep, voteType, submitCode, sectionStepId);
                return Response.ok("发起投票成功");
            } else {
                return Response.fail("获取数据失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("发起投票失败");
        }
    }

    private void sendMsg(TSUser tsUser,BeSectionStep beSectionStep, String voteType, String submitCode, String sectionStepId) throws Exception {
        List<String[]> md5ByIdCards = beSectionExpertInfoService.findMd5ByLoginNames(beSectionStep.getSectionId());
        for (BeSectionExpertInfo beSectionExpertInfo : beSectionExpertInfoService
                .getExpertListBySectionId(beSectionStep.getSectionId(), voteType, submitCode)) {
            if (WebSocketCached.getExpertChannelContextMap().get(beSectionExpertInfo.getSeqId()) != null) {
                //返回重评开始环节名称和发起重评专家的姓名
                if ("2".equals(voteType)) {
                    webSocketService.send(
                            WebSocketCached.getExpertChannelContextMap().get(beSectionExpertInfo.getSeqId()),
                            new ResponseObject(WebSocketOpTypeEnum.CP_START_VOTE).setData("stepName", beSectionStep.getStepName()
                            ).setData("expertName", tsUser.getUserName()).setData("sectionStepId", sectionStepId)
                                    .setTodoList(md5ByIdCards));
                } else {
                    webSocketService.send(
                            WebSocketCached.getExpertChannelContextMap().get(beSectionExpertInfo.getSeqId()),
                            new ResponseObject(WebSocketOpTypeEnum.TB_START_VOTE).setTodoList(md5ByIdCards));
                }
            }
        }
    }

    @GetMapping("/getVoteResultByLoginName")
    @ApiOperation(value = "推选组长-查询当前登录专家推选的专家id", response = BeExpertVoteResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true)})
    public Response<BeExpertVoteResult> getVoteResultByLoginName(HttpServletRequest request, String sectionId,
                                                                 String voteType) {
        try {
            String userGuid = request.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                BeSectionExpertInfo beSectionExpertInfo = beSectionExpertInfoService.selectByLoginName(sectionId,
                        tsUser.getLoginName());
                return Response.ok(beExpertVoteResultService.getBeExpertVoteResult(beSectionExpertInfo.getSeqId(),
                        sectionId, voteType));
            } else {
                return Response.fail("获取专家账号登录信息失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("获取当前登录专家推选的专家id失败");
        }
    }

    @GetMapping("/queryResultByExpertId")
    @ApiOperation(value = "投票结果-查询当前登录专家的投票结果（用于重评和替补）", response = BeExpertVoteResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true)})
    public Response<BeExpertVoteResult> queryResultByExpertId(HttpServletRequest request, String sectionId,
                                                              String voteType) {
        try {
            String userGuid = request.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                BeSectionExpertInfo beSectionExpertInfo = beSectionExpertInfoService.selectByLoginName(sectionId,
                        tsUser.getLoginName());
                String submitCode = null;
                if ("2".equals(voteType)||"1".equals(voteType)) {
                    BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(sectionId);
                    submitCode = beSectionMethod.getSubmitCode();
                }
                BeExpertVoteResult beExpertVoteResult = new BeExpertVoteResult();
                beExpertVoteResult.setExpertId(beSectionExpertInfo.getSeqId());
                beExpertVoteResult.setSectionId(sectionId);
                beExpertVoteResult.setVoteType(voteType);
                beExpertVoteResult.setSubmitCode(submitCode);
                return Response.ok(beExpertVoteResultService.queryResultByExpertId(beExpertVoteResult));
            } else {
                return Response.fail("获取专家账号登录信息失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("获取当前登录专家推选的专家id失败");
        }
    }

    @GetMapping("/getVoteResultDetailList")
    @ApiOperation(value = "推选组长-获取当前标段专家投票列表", response = BeExpertVoteResultVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true)})
    public Response<List<BeExpertVoteResultVO>> getVoteResultDetailList(String sectionId, String voteType) {
        try {
            List<BeExpertVoteResultVO> beExpertVoteResultVOList = new ArrayList<BeExpertVoteResultVO>();
            BeExpertVoteResultVO beExpertVoteResultVO = null;
            String submitCode = null;
            if ("2".equals(voteType)) {
                BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(sectionId);
                submitCode = beSectionMethod.getSubmitCode();
            }
            for (Map<String, Object> voteResultMap : beExpertVoteResultService.getVoteResultDetailList(sectionId,
                    voteType, submitCode)) {
                beExpertVoteResultVO = new BeExpertVoteResultVO();
                beExpertVoteResultVO.setExpertId(StringUtil.toString(voteResultMap.get("expertId")));
                beExpertVoteResultVO.setExpertName(StringUtil.toString(voteResultMap.get("expertName")));
                beExpertVoteResultVO.setVoteResult(StringUtil.toString(voteResultMap.get("voteResult")));
                beExpertVoteResultVOList.add(beExpertVoteResultVO);
            }
            return Response.ok(beExpertVoteResultVOList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail(e.getMessage());
        }
    }

    @GetMapping("/getVoteResultList")
    @ApiOperation(value = "推选组长-获取当前标段专家投票结果列表", response = VoteResultDto.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true)})
    public Response<List<VoteResultDto>> getVoteResultList(String sectionId, String voteType) {
        try {
            return Response.ok(beExpertVoteResultService.getVoteResultList(sectionId, voteType));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail(e.getMessage());
        }
    }

    @PostMapping("/isRepeatedVotes")
    @ApiOperation(value = "推选组长-判断是否投票结果是否重复(2019-12-02-v1)", response = Boolean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true)})
    public Response<Boolean> isRepeatedVotes(String sectionId, String voteType) throws Exception {
        try {
            Boolean result = beExpertVoteResultService.isRepeatedVotes(sectionId, voteType);
            if (result) {
                String submitCode = null;
                if ("2".equals(voteType)) {
                    BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(sectionId);
                    submitCode = beSectionMethod.getSubmitCode();
                }

                List<String[]> idCards = beSectionExpertInfoService.findMd5ByLoginNames(sectionId);
                for (BeSectionExpertInfo beSectionExpertInfo : beSectionExpertInfoService
                        .getExpertListBySectionId(sectionId, voteType, submitCode)) {
                    webSocketService.send(
                            WebSocketCached.getExpertChannelContextMap().get(beSectionExpertInfo.getSeqId()),
                            new ResponseObject(WebSocketOpTypeEnum.EXPERT_REPEAT).setTodoList(idCards));
                }
            }
            return Response.ok(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("判断是否投票结果是否重复失败");
        }
    }

    @PostMapping("/delRepeatedVotes")
    @ApiOperation(value = "推选组长-若有重复清除投票结果进行重新投票(2019-12-02-v1)", response = Boolean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true)})
    public Response<Boolean> delRepeatedVotes(String sectionId, String voteType) throws Exception {
        try {
            Boolean result = beExpertVoteResultService.isRepeatedVotes(sectionId, voteType);
            if (result) {
                //清除数据
                return Response.ok(beExpertVoteResultService.delRepeatedVotes(sectionId, voteType)>0);
            }
            return Response.ok(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("清除投票结果失败");
        }
    }

    @PostMapping("/delBySectionId")
    @ApiOperation(value = "重评时专家投票结果为不同意时清空", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true)})
    public Response<String> delBySectionId(String sectionId, String voteType) throws Exception {
        try {
            if (StringUtils.isBlank(sectionId) || StringUtils.isBlank(voteType)) {
                return Response.fail("参数不可以为空");
            }
            String submitCode = null;
            if ("2".equals(voteType)) {
                BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(sectionId);
                submitCode = beSectionMethod.getSubmitCode();
            }
            if (beExpertVoteResultService.delBySectionId(sectionId, voteType, submitCode) < 1) {
                return Response.fail("删除专家投票结果失败");
            }
            return Response.ok("删除专家投票结果删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("删除专家投票结果失败");
        }
    }

    @PostMapping("/voteExpertByReappraisal")
    @ApiOperation(value = "重评功能-专家投票（仅用于重评）(2019-10-29-v1)", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionStepId", value = "标段环节主键", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteType", value = "投票类型0专家选举 1替补 2重评", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "voteResult", value = "推选组长专家id,pk-bs_section_expert-seq_id,替补和重评:1同意 0 不同意", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_ONE, message = "参数不可以为空"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_TWO, message = "重评专家投票失败"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_THREE, message = "获取专家账号登录信息失败"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_FOUR, message = "重评专家投票异常"),
    })
    public Response<String> voteExpertByReappraisal(@RequestParam("sectionId") String sectionId, @RequestParam("sectionStepId") String sectionStepId,
                                                    @RequestParam("voteType") String voteType, @RequestParam("voteResult") String voteResult) {
        try {
            if (StringUtils.isBlank(sectionId) || StringUtils.isBlank(sectionStepId)
                    || StringUtils.isBlank(voteType) || StringUtils.isBlank(voteResult)) {
                return Response.fail("参数不可以为空", Constants.FIVE_HUNDRED_AND_ONE);
            }
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                BeSectionExpertInfo _beSectionExpertInfo = beSectionExpertInfoService.selectByLoginName(sectionId,
                        tsUser.getLoginName());
                if (beExpertVoteResultService.voteExpert(_beSectionExpertInfo.getSeqId(), sectionId, voteResult,
                        sectionStepId, voteType, Constants.ONE)) {
                    //重评需要根据submitCode去查询数据（每重评一次submitCode都会变）
                    BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(sectionId);
                    String submitCode = beSectionMethod.getSubmitCode();
                    BeSectionStep beSectionStep = beSectionStepService.selectByPrimaryKey(sectionStepId);
                    //判断所有专家是否投票完成，完成则计算少数服从多数
                    String computerVoteResult = beExpertVoteResultService.computerVoteResult(sectionId, voteType, submitCode);
                    if ("1".equals(computerVoteResult)) {
                        //多数专家同意重评，发起数据更新操作
                        //查询需要重评的环节
                        //用于判断需要更新环节信息的标识
                        boolean isUpdate = false;
                        //重评成功,重新生产提交版本号 added by daizhifeng 20191219
                        String newSubmitCode = IdsUtil.uuid();
                        List<Map<String, Object>> list = beSectionStepService.getBeSectionStepReappraisalList(sectionId);
                        for (Map<String, Object> dataMap : list) {
                            //删除所有选中环节的评审记录（软删除）并更新评审进度和环节任务执行状态
                            //更新从选中的第一个环节到最后一个环节
                            if (sectionStepId.equals(OConvertUtils.getString(dataMap.get("sectionStepId")))) {
                                isUpdate = true;
                            }
                            if (isUpdate) {
                                log.info("**********重评删除数据开始环节id:" + sectionStepId);
                                String stepCode = OConvertUtils.getString(dataMap.get("stepCode"));
                                beSectionExpertProgressService.updateStatusById(sectionId, stepCode, "0", newSubmitCode);
                            }
                        }
                        //更新submitCode
                        beSectionMethodService.updateByPrimaryKeySelective(sectionId, newSubmitCode);
                        //更新重评替补成功状态表
                        beSectionReappraisalRecordService.updateRecordByObject(sectionId, submitCode, voteType, sectionStepId);
                    } else if ("0".equals(computerVoteResult)) {
                        //多数专家不同意重评，发起本次投票记录删除操作
                        beExpertVoteResultService.delBySectionId(sectionId, voteType, submitCode);
                        BeSectionReappraisalRecord record = new BeSectionReappraisalRecord();
                        record.setSectionId(sectionId);
                        record.setSubmitType(voteType);
                        record.setIsSuccess(Constants.FALSE);
                        record.setSubmitCode(beSectionMethod.getSubmitCode());
                        beSectionReappraisalRecordService.deleteRecordByObject(record);
                    }
                    for (BeSectionExpertInfo beSectionExpertInfo : beSectionExpertInfoService
                            .getExpertListBySectionId(sectionId, voteType, submitCode)) {
                        if (WebSocketCached.getExpertChannelContextMap().get(beSectionExpertInfo.getSeqId()) != null) {
                            List<String[]> idCards = beSectionExpertInfoService.findMd5ByLoginNames(sectionId);
                            webSocketService.send(
                                    WebSocketCached.getExpertChannelContextMap().get(beSectionExpertInfo.getSeqId()),
                                    new ResponseObject(WebSocketOpTypeEnum.EXPERT_COUNT)
                                            .setData("stepCode", beSectionStep.getStepCode())
                                            .setData("computerVoteResult", computerVoteResult).setTodoList(idCards)
                            );
                        }
                    }
                    return Response.ok("重评专家投票成功");
                } else {
                    return Response.fail("重评专家投票失败", Constants.FIVE_HUNDRED_AND_TWO);
                }
            } else {
                return Response.fail("获取专家账号登录信息失败", Constants.FIVE_HUNDRED_AND_THREE);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("重评专家投票异常", Constants.FIVE_HUNDRED_AND_FOUR);
        }
    }


    public static void main(String[] args) {
        System.out.println(new Date(1571822576949L));
        System.out.println(new Date(1571822577000L));
    }
}
